# encoding: utf-8
class Report::ComplaintReportController < ApplicationController
  def show
    @title_view = 'Denuncias'
    @complaint_types = RequestAndComplaint::ComplaintType.all
  end
  
  def show_pdf
    
    search_complaint_type = params[:search_complaint_type]
    search_state = params[:search_complaint_state]

    request_and_complaint_registration_requests = RequestAndComplaint::Complaint.all

    if ! search_complaint_type.blank?
      if ! search_state.blank?
        request_and_complaint_registration_requests = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.complaint_type = ? and request_and_complaint_complaints.state = ?", search_complaint_type, search_state])
      else
        request_and_complaint_registration_requests = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.complaint_type = ? ", search_complaint_type])
      end

    else
      if !search_state.blank?
        request_and_complaint_registration_requests = RequestAndComplaint::Complaint.find(:all,:conditions=>["request_and_complaint_complaints.state = ?", search_state])
      end
    end
   

    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    #    Titulo del reporte
    pdf.text_box "Reporte de Denuncias",
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20
    #    Cuerpo   
   
    pdf.move_down 150
    
    arr = Array.new
    
    #Colocando headers
    arr[0] = Array.new
    arr[0][0] = "Id"
    arr[0][1] = "Tipo de denuncia"
    arr[0][2] = "Nombre"
    arr[0][3] = "DNI"
    arr[0][4] = "Descripción"
    arr[0][5] = "Estado"
    arr[0][6] = "Fecha"

    j = 1

    request_and_complaint_registration_requests.each {
      |complaint|

      arr[j] = Array.new
      arr[j][0] = complaint.id.to_s
      arr[j][1] = complaint.obj_complaint_type.name
      arr[j][2] = complaint.name
      arr[j][3] = complaint.dni
      arr[j][4] = complaint.description
      arr[j][5] = complaint.str_state
      arr[j][6] = complaint.created_at.strftime("%d/%m/%Y").to_s
      j = j + 1
    }

    pdf.table arr, :column_widths => [20, 80, 100, 80, 100, 70, 90], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    #Pie de pagina
    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end
    #Nombre de reporte
    send_data pdf.render, :filename => "Reporte de Denuncias" << ".pdf", :type => "application/pdf"
  end  
end
